package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.InventoryDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.*;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.dao.InventoryLogDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("inventoryLogService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class InventoryLogService extends BaseServiceImpl<InventoryLog> {

	@Resource
	private InventoryLogDao inventoryLogDao;
	@Resource
	private UserDao userDao;
	@Resource
	private InventoryDao inventoryDao;

	@Override
	protected BaseDao <InventoryLog> getDao() {
		return inventoryLogDao;
	}
	/*
	*获取库存日志列表
	* zlq
	* page:页码
	* token：
	* inventoryId：库存记录ID
	* typeValue：类型 1：+ 2：-
	* inventorySourceValue：来源 1：采购 2：线下核销 3：订单购买 4： 取消订单 5：驳回订单 6：自营调货 7：库存盘点
	 */
	public String getLogList(ModelMap model, String page, String token, String inventoryId,String typeValue,String inventorySourceValue){
		PageView pageview = new PageView();
		int pageNo = 1;
		int pageSize = 10;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if(!NumberUtils.isDigits(inventoryId)){
			return CodeContent.getModelByCode(0, "库存ID不能为空", model);
		}
		Inventory inventory=inventoryDao.getById(Integer.parseInt(inventoryId));
		if(inventory==null){
			return CodeContent.getModelByCode(0, "库存不存在", model);
		}

		InventoryLog inventoryLog=new InventoryLog();
		inventoryLog.setInventoryId(inventory.getId());
		if(NumberUtils.isDigits(typeValue)){
			inventoryLog.setTypeValue(Integer.parseInt(typeValue));
		}
		if(NumberUtils.isDigits(inventorySourceValue)){
			inventoryLog.setInventorySourceValue(Integer.parseInt(inventorySourceValue));
		}
		pageview=inventoryLogDao.queryPageView(pageNo, pageSize,inventoryLog);
		model.put("code",1);
		model.put("msg","success");
		if (pageview != null){
			model.put("datas",convertList(pageview.getRecords()));
			model.put("recordsTotal", pageview.getRowCount());
			model.put("totalPage", pageview.getPageCount());
			model.put("pageNow", pageview.getPageNow());
		}

		return new JSONObject(model).toString();
	}

	public List<ModelMap> convertList(List<InventoryLog> inventoryLogList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (inventoryLogList != null && inventoryLogList.size() > 0) {
			for (InventoryLog inventoryLog : inventoryLogList) {
				ModelMap map = buildModelList(inventoryLog);
				list.add(map);
			}
		}
		return list;
	}

	public ModelMap buildModelList(InventoryLog inventoryLog) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		ModelMap model = new ModelMap();
		model.put("id",inventoryLog.getId());
		model.put("inventorySource",inventoryLog.getInventorySource().toString());
		model.put("changeNum",inventoryLog.getType().toString() + inventoryLog.getChangeNum());
		model.put("code",inventoryLog.getCode());
		model.put("createTime",formatter.format(inventoryLog.getCreateTime()));
		model.put("remark",inventoryLog.getRemark()!=null?inventoryLog.getRemark():"");
		User user=userDao.getById(inventoryLog.getUserId());
		model.put("userName",user!=null?user.getRealName():"");

		return  model;
	}
}